/****************************************************
          File Name:34-1.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sun 31 Aug 2014 07:09:03 PM CST
*****************************************************/

#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
#include<errno.h>
#include<wait.h>

int main(int argc,char *argv[])
{
    int i;
        for(i=0;i<argc;i++)
        {
            printf("%d\n",i);
            switch(fork())
            {
                case -1:
                    exit(1);
                case 0:
                    sleep(10);
                    break;
                default:
                    signal(SIGUSR1,SIG_IGN);
                    sleep(5);
                    killpg(getpgrp(),SIGUSR1);
                    if(errno)
                    {
                        perror("kill");
                        exit(1);
                    }
                    wait(NULL);
            }
            sleep(8);
        } 
    exit(0);
}
